<template>
  <div class="flexContent">
    <div class="searchBox">

      <el-form class="demo-form-inline" label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="账号">
                  <el-input v-clearEmoij v-model.trim="userName" clearable style="width: 95%" placeholder="账号"
                    @keyup.enter.native="searchFn" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="类型">
                  <el-select v-model="type" popper-class="eloption" placeholder="请选择..." style="width: 95%">
                    <el-option value="0" label="供应链" />
                    <el-option value="1" label="公司" />
                    <el-option value="2" label="门店" />
                    <el-option value="3" label="供应商" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="searchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetFn" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="add">
      <el-button class="addBtn" @click="addFn">新增</el-button>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <el-table border class="tablebox" :data="userList" style="width: 100%" height="100%" size="medium">
          <el-table-column type="index" align="center" width="80" label="序号" />
          <el-table-column prop="userName" align="center" label="用户名" />
          <el-table-column prop="status" align="center" label="状态">
            <template v-slot="{ row }">
              <span v-if="row.status === '0'" style="color: #67C23A">正常</span>
              <span v-if="row.status === '1'" style="color: #F56C6C">禁用</span>
            </template>
          </el-table-column>
          <el-table-column prop="type" align="center" label="类型">
            <template v-slot="{ row }">
              <span v-if="row.type === '0'">供应链</span>
              <span v-if="row.type === '1'">公司</span>
              <span v-if="row.type === '2'">门店</span>
              <span v-if="row.type === '3'">供应商</span>
            </template>
          </el-table-column>
          <el-table-column prop="storeName" align="center" label="所属门店" />
          <el-table-column prop="supplierName" align="center" label="所属供应商" />
          <el-table-column align="center" fixed="right" width="250" label="操作">
            <template v-slot="{ row }">
              <el-button type="text" style="margin-right: 10px; color: rgb(0, 132, 255)"
                @click.stop="getDetail(row)">详情</el-button>
              <el-button slot="reference" style="color: rgb(0, 132, 255)" type="text"
                @click.stop="edit(row)">修改</el-button>
              <!-- <el-button type="text" v-if="row.isFlag === '0'" style="margin-right: 10px; color: red"
                @click="resetPwdFn(row)">重置密码</el-button> -->
              <el-button type="text" v-if="row.status === '1'" style="margin-right: 10px; color: red"
                @click="$delFn('用户', del, row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,-> , sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
    <AddDialog :close-on-click-modal="false" :show-dialog.sync="showDialog" :title="title" @btnOk="btnOk" @close="close">
      <el-form ref="addFormRef" :disabled="title === '详情'" :model="addForm" :rules="addRules" label-width="100px">
        <el-form-item label="账号" prop="userName">
          <el-input v-clearEmoij v-model.trim="addForm.userName" :disabled="title === '修改'" placeholder="请输入..." />
        </el-form-item>
        <el-form-item label="密码" prop="password2" v-if="title === '新增'">
          <el-input v-clearEmoij v-model.trim="addForm.password2" show-password placeholder="请输入..." />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="addForm.type" popper-class="eloption" placeholder="请选择...">
            <el-option value="0" label="供应链" />
            <el-option value="1" label="公司" />
            <el-option value="2" label="门店" />
            <el-option value="3" label="供应商" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="addForm.status" size="small">
            <el-radio label="0">正常</el-radio>
            <el-radio label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="addForm.type == '2'" label="绑定门店" prop="storeId">
          <el-select v-model="addForm.storeId" popper-class="eloption" filterable placeholder="请选择...">
            <el-option v-for="item in storeList" :key="item.id" :value="item.id" :label="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item label="绑定供应商" v-if="addForm.type == '3'" prop="supplierId">
          <el-select v-model="addForm.supplierId" popper-class="eloption" filterable placeholder="请选择...">
            <el-option v-for="item in scmList" :key="item.id" :value="item.id" :label="item.name" />
          </el-select>
        </el-form-item>
      </el-form>
    </AddDialog>
  </div>
</template>

<script>
import md5 from "md5";
import AddDialog from "@/components/Goods/AddDialog";
import { getShopEasy } from '@/api/shop';
import { getScreenUserInfo, getSupplierList, createScreenUser, updateScreenUser, deleteScreenUser } from "@/api/userInfo";
export default {
  name: "UserInfo",
  components: {
    AddDialog,
  },
  data() {
    return {
      width: "40%",
      title: "新增",
      showDialog: false,
      dialogFormVisible: false,
      addForm: {
        userName: "",
        status: "0",
        storeId: "",
        supplierId: "",
        password2: "",
        type: "",
      },
      pwdForm: {
        pwd: "",
      },
      addRules: {
        userName: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            pattern: /^[a-zA-Z0-9]{5,20}$/,
            message: "用户名应为5-20位字母和数字",
            trigger: "blur",
          },
        ],
        status: [
          {
            required: true,
            message: "请选择用户类型",
            trigger: "blur",
          },
        ],
        type: [
          {
            required: true,
            message: "请选择用户状态",
            trigger: "blur",
          },
        ],
        supplierId: [
          {
            required: true,
            message: "请选择供应商",
            trigger: "blur",
          },
        ],
        storeId: [
          {
            required: true,
            message: "请选择门店",
            trigger: "blur",
          },
        ],
        password2: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            min: 6,
            max: 16,
            message: "长度在6到16个字符",
            trigger: "blur",
          },
          {
            pattern: /^[A-Za-z0-9]+$/,
            message: "请输入数字或字母",
            trigger: "blur",
          },
        ],
      },
      scmList: [],
      storeList: [],
      type: "",
      userName: "",
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      isSearch: false,
      userList: [],
      userId: "",
    };
  },
  created() {
    this.getSupplierList();
    this.getScreenUserInfo();
    this.getShopEasy();
    // console.log(md5('12345678'))
  },
  methods: {
    async getScreenUserInfo() {
      const serData = {
        userName: this.userName ? this.userName : "",
        type: this.type ? this.type : "",
      };
      if (!this.isSearch) {
        const serData = {
          userName: "",
          type: "",
        };
      }
      const res = await getScreenUserInfo({ ...this.page, ...serData });
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        console.log(result);
        this.userList = result.list;
        this.page.pageSize = result.pageSize;
        this.page.pageNum = result.pageNum;
        this.page.total = result.total;
        console.log("userList", this.userList);
      } else {
        // // this.$message.error(res.message)
      }
    },
    async getSupplierList() {
      const res = await getSupplierList(this.page);
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        this.scmList = result;
        console.log("scmList", this.scmList);
      } else {
        // // this.$message.error(res.message)
      }
    },
    async getShopEasy() {
      const res = await getShopEasy();
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        console.log('shopList', result);
        this.storeList = result.store;
        console.log('shopList', this.storeList);
      } else {
        // this.$message.error(res.message);
      }
    },
    addFn() {
      this.title = "新增";
      this.showDialog = true;
      // this.getscmListEasy();
      // this.getRoleListEasy();
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      this.getScreenUserInfo();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.pageNum = val;
      this.getScreenUserInfo();
    },
    close() {
      console.log("close");
      this.showDialog = false;
      this.addForm = {
        userName: "",
        status: "0",
        storeId: "",
        supplierId: "",
        password2: "",
        type: "",
      };
      this.$refs.addFormRef.resetFields();
    },
    btnOk() {
      // 判断是新增还是修改
      // const password = md5(this.addForm.password)
      if (this.title === "新增") {
        // 新增
        this.$refs.addFormRef.validate(async (isOk) => {
          if (isOk) {
            //
            const password = md5(this.addForm.password2);
            const res = await createScreenUser({
              ...this.addForm,
              password: password,
            });
            if (res.code === 200) {
              this.$message.success("新增成功");
              this.getScreenUserInfo();
              this.close();
            } else {
              // this.$message.error(res.message);
            }
          } else {
            this.$message.warning("请检查输入信息");
          }
        });
      } else if (this.title === "详情") {
        // 关闭
        this.close();
      } else if (this.title === "修改") {
        // 修改
        this.$refs.addFormRef.validate(async (isOk) => {
          if (isOk) {
            console.log(this.addForm.password2);
            // const password = md5(this.addForm.password2)
            const res = await updateScreenUser({
              ...this.addForm,
              id: this.userId,
            });
            if (res.code === 200) {
              this.$message.success("修改成功");
              this.getScreenUserInfo();
              this.close();
            } else {
              // this.$message.error(res.message);
            }
          } else {
            this.$message.warning("请检查输入信息");
          }
        });
      }
    },
    async getDetail(row) {
      this.title = "详情";
      this.getSupplierList();
      this.getShopEasy();
      this.addForm.userName = row.userName;
      this.addForm.status = row.status;
      this.addForm.storeId = row.storeId;
      this.addForm.supplierId = row.supplierId;
      this.addForm.type = row.type;
      this.showDialog = true;
    },
    async edit(row) {
      this.title = "修改";
      console.log(row);
      this.getSupplierList();
      this.getShopEasy();
      this.addForm.userName = row.userName;
      this.addForm.status = row.status;
      this.addForm.storeId = row.storeId;
      this.addForm.supplierId = row.supplierId;
      this.addForm.type = row.type;
      this.showDialog = true;
      // this.addForm = row
      // this.pwdForm.pwd = "";
      this.userId = row.id;
    },
    async del(row) {
      const res = await deleteScreenUser({ id: row.id });
      if (res.code === 200) {
        this.$message.success("删除成功");
        if (this.userList.length === 1 && this.page.pageNum > 1)
          this.page.pageNum--;
        this.getScreenUserInfo();
      } else {
        // this.$message.error(res.message);
      }
    },
    async searchFn() {
      this.page.pageNum = 1;
      this.page.pageSize = 20;
      this.isSearch = true;
      this.getScreenUserInfo();
    },
    resetFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = false;
      this.type = "";
      this.userName = "";
      this.getScreenUserInfo();
    },
    // selstoreManage() {
    //   console.log(this.addForm.storeManage);
    //   this.addForm.roleIdList = "";
    //   this.getRoleListEasy(this.addForm.storeManage);
    // },
  },
};
</script>

<style lang="scss" scoped></style>
